function gadget:GetInfo()           
  return {
    name      = "Start Handler",
    desc      = "Handles everything at Start of game",
    author    = "SkyStar",
    date      = "June 6, 2010",
    license   = "GNU GPL, v2 or later",
    layer     = 0,
    enabled   = true
  }
end

if (gadgetHandler:IsSyncedCode()) then

--------------------------------------
--------------- SYNCED ---------------
--------------------------------------

local gaiaTeamID = Spring.GetGaiaTeamID()
local endGame = false
local debaser = {}

--------------------------------------

	function gadget:Initialize() -- Everything as soon as Gadget loads
	-- Prepare Global Framework functions --
	Gfunction = _G.functions
	luaAITeams = Gfunction.getLuaAITeams()
	end -- Initialize()
	
	function gadget:GameStart() -- Everything as soon and the game starts
	-- End game if there aren't two player ally teams --
	local playerAllies = Spring.GetAllyTeamList()
		if (#playerAllies - 1 ) ~= 2 then		
		endGame = true
		SendToUnsynced("endGame", endGame)
		end	
		-- End game if there aren't two LuaAI -	
		if(#luaAITeams ~= 2) then		
		endGame = true
		SendToUnsynced("endGame", endGame)
		end			
		-- COMPATIBILITY when Spring stops spawning start units --
		if (#Spring.GetAllUnits() == 0) then 		
		local teams = Spring.GetTeamList()	
			for i = 1,#teams do
			local teamID = teams[i]
				if (teamID ~= gaiaTeamID) then
				SpawnStartUnit(teamID)
				end
			end
		end	
	-- Stuff that should happen regardless of anything --
	local teams = Spring.GetTeamList()	
		for i = 1,#teams do
		local teamID = teams[i]
		-- Spawn Bases for LuaAI --
			if(luaAITeams[teamID]) then
			local allyID = luaAITeams[teamID]
			local xmin, zmin, xmax, zmax = Spring.GetAllyTeamStartBox(allyID)
			if (not xmin or not xmax or not zmin or not zmax) then return end
			local facing = Gfunction.getFacing((xmin+xmax)/2,(zmin+zmax)/2)					
				if(facing == "north") then
				posX = (xmin+xmax)/2
				posZ = zmax - 50
				elseif facing == "south" then
				posX = (xmin+xmax)/2
				posZ = zmin + 50
				elseif facing == "east" then
				posX = xmin + 50
				posZ = (zmin+zmax)/2
				elseif facing == "west" then
				posX = xmax - 50
				posZ =(zmin+zmax)/2
				end		
			local posY = Spring.GetGroundHeight(posX, posZ)
			Gfunction.createUnit("debasec", posX, 0, posZ, facing, teamID, true)
			-- Send to framework for storage --
			Spring.SendLuaRulesMsg("Gfunction:"..'basePos'..','..posX..','..posY..','..posZ..','..allyID)
			end
		-- Spawn nullbase --
			if (teamID ~= gaiaTeamID and not luaAITeams[teamID]) then	
			Gfunction.createUnit("nullbase", 0, 0, 0, "north", teamID, false)
			end		
		-- Spawn Creep Base --	
			if(not luaAITeams[teamID] and teamID ~= gaiaTeamID) then 
			local x,y,z = Spring.GetTeamStartPosition(teamID)
			local facing = Gfunction.getFacing(x,z)
			local _,_,_,_,_,allyID = Spring.GetTeamInfo(teamID)
			local AIAlly = Gfunction.getLuaAIAllies()
			Gfunction.createUnit("debaser", x, 0, z, facing, AIAlly[allyID], true) 
			end	
		-- Remove resources --
		Spring.SetTeamResource(teamID, "ms", 0)
		Spring.SetTeamResource(teamID, "m", 0)
		Spring.SetTeamResource(teamID, "es", 0)
		Spring.SetTeamResource(teamID, "e", 0)	
		end
	end -- GameStart()
	
	
function gadget:UnitCreated(unitID,unitDefID,unitTeam) -- Handle stuff that spawns at gamestart
	local time = math.floor(Spring.GetGameSeconds())
-- Don't do if were started already --
	if time > 0.5 then return end
-- Handle the nulls --
	if Gfunction.getUnitDef(unitID).name == "nullbase" or luaAITeams[unitTeam] and Gfunction.isEngine(unitID) then
	Spring.MoveCtrl.Enable(unitID)
	Spring.MoveCtrl.SetNoBlocking(unitID, true)
	Spring.MoveCtrl.SetPosition(unitID, Game.mapSizeX+4000, -10000, Game.mapSizeZ+4000)
	Spring.SetUnitCloak(unitID, true)
	Spring.SetUnitHealth(unitID, {paralyze=99999999})
	Spring.SetUnitNoDraw(unitID, true)
	Spring.SetUnitStealth(unitID, true)
	Spring.SetUnitNoSelect(unitID, true)
	Spring.SetUnitNoMinimap(unitID, true)
	return
	end
-- Move the engines a bit --
	if Gfunction.isEngine(unitID) then
	local posx,posy,posz = Spring.GetUnitPosition(unitID)
	posx,posy,posz = Gfunction.towardsMapCenter(posx,posy,posz,50)
	Spring.SetUnitPosition(unitID, posx , posy, posz)
	end
end -- UnitCreated()


	function gadget:GameFrame(frame) -- Everything while game is not paused
	local time = math.floor(Spring.GetGameSeconds())
	-- 	Wait 5 seconds --
		if time == 5 then
		-- If the game setup is fail then destory everything --
			if endGame then
				for _, unitID in ipairs(Spring.GetAllUnits()) do 
				Gfunction.destroyUnit(unitID, false)
				end
			return
			end
		end	
	end -- GameFrame()
	
	function GetStartUnit(teamID) -- Figure out our starting unit
	local side = select(5, Spring.GetTeamInfo(teamID))
	local startUnit
		if (side == "") then
		local sidedata = Spring.GetSideData()
			if (sidedata and #sidedata > 0) then
			startUnit = sidedata[1 + teamID % #sidedata].startUnit
			end
		else
		startUnit = Spring.GetSideData(side)
		end
	return startUnit
	end -- GetStartUnit()
	
	function SpawnStartUnit(teamID) -- Create the starting unit at the correct position
	local startUnit = GetStartUnit(teamID)
		if (startUnit and startUnit ~= "") then
		local x,y,z = Spring.GetTeamStartPosition(teamID)
		local facing = Gfunction.getFacing(x,z)
		Gfunction.createUnit(startUnit, x, y, z, facing, teamID, true)
		end
	end -- SpawnStartUnit()

		
end   -- SYNCED

if (not gadgetHandler:IsSyncedCode()) then

local endGame = false

---------------------------------------
-------------- UN-SYNCED --------------
---------------------------------------

	function RecvFromSynced(_,state) -- Recieve Synced Commands
		if state == "endGame" then
		endGame = true
		end
	end -- RecvFromSynced()

	function gadget:DrawScreen()
	local time = math.floor(Spring.GetGameSeconds())	
		if(time == 0) then
		local vsx, vsy = gl.GetViewSizes()
		local posx = vsx * 0.5
		local posy = vsy * 0.2		
		gl.Text("Your start point is also your creep portal, choose CAREFULLY", posx, posy, 30, "ocn")
		gl.Text("Pro Tip: Start near an open area or path", posx, posy - 30, 30, "ocn")
		return
		end		
		if(time ~= 0 and endGame) then
		local vsx, vsy = gl.GetViewSizes()
		local posx = vsx * 0.5
		local posy = vsy * 0.5		
		gl.Text("The game is set up WRONG!!! Everything will soon explode", posx, posy, 30, "ocn")
		gl.Text("Check the number of AI (must be 1 for each team of players)", posx, posy - 30, 30, "ocn")
		gl.Text("Check the number of Player Teams (must be 2 teams of players)", posx, posy - 60, 30, "ocn")
		end
	end -- DrawScreen()
	
end -- UNSYNCED